from django.shortcuts import render
from django.http import HttpResponse
from django.db.models import Q
from .models import Article
from .models import Classify
from .models import City
from .models import Score
from .models import UserInfo
from .models import Site
from .models import User
# Create your views here.
def index(request):
    sify=request.POST.get('sify')
    print(sify)
    place=request.POST.get('place')
    print(place)
    money=request.POST.get('money')
    print(money)
    text=request.POST.get('text')
    print(text)


    if money =='1':
        ao=0
        bo=200
    elif money=='2':
        ao = 200
        bo = 500
    elif money=='3':
        ao = 500
        bo = 1000
    elif money=='4':
        ao = 1000
        bo = 2500
    elif money=='5':
        ao = 2500
        bo = 5000
    elif money=='6':
        ao = 5000
        bo = 10000
    if text!=None:
        art=Article.objects.filter(Q(a_title__icontains=text)|Q(a_author__icontains=text)|
                                    Q(a_abstract__icontains=text)|Q(site__si_name_icontains=text))\
            .values('a_cover','a_title','a_author','a_abstract','a_date','site__si_name','site__si_score','site__si_web')
    if sify==None and place==None and money==None:
        art = Article.objects.values\
            ('a_cover','a_title','a_author','a_abstract','a_date','site__si_name','site__si_score','site__si_web')
    elif sify != None and place == None and money==None:
        art = Article.objects.filter(city_id=sify).values\
            ('a_cover','a_title','a_author','a_abstract','a_date','site__si_name','site__si_score','site__si_web')
    elif sify == None and place != None and money == None:
        art = Article.objects.filter(site_id=place).values \
            ('a_cover', 'a_title', 'a_author', 'a_abstract', 'a_date', 'site__si_name', 'site__si_score', 'site__si_web')
    elif sify == None and place == None and money != None:
        art = Article.objects.filter(site__si_score__gte=ao,site__si_score__lte=bo).values \
            ('a_cover', 'a_title', 'a_author', 'a_abstract', 'a_date', 'site__si_name', 'site__si_score', 'site__si_web')

    elif sify != None and place != None and money == None:
        art = Article.objects.filter(city_id=sify,site_id=place).values \
            ('a_cover', 'a_title', 'a_author', 'a_abstract', 'a_date', 'site__si_name', 'site__si_score', 'site__si_web')
    elif sify != None and place == None and money != None:
        art = Article.objects.filter(city_id=sify,site__si_score__gte=ao, site__si_score__lte=bo).values \
            ('a_cover', 'a_title', 'a_author', 'a_abstract', 'a_date', 'site__si_name', 'site__si_score', 'site__si_web')
    elif sify == None and place != None and money != None:
        art = Article.objects.filter(site_id=place, site__si_score__gte=ao, site__si_score__lte=bo).values \
            ('a_cover', 'a_title', 'a_author', 'a_abstract', 'a_date', 'site__si_name', 'site__si_score',
             'site__si_web')

    else:
        art = Article.objects.filter(city_id=sify,site_id=place,site__si_score__gte=ao,site__si_score__lte=bo).values\
            ('a_cover', 'a_title', 'a_author','a_abstract', 'a_date', 'site__si_name','site__si_score','site__si_web')
    article={'art':art}
    return render(request, 'it_mjj/index.html',article)
def screen(request):
    sify1 = request.POST.get('sify')
    print(sify1)
    place1 = request.POST.get('place')
    print(place1)
    money1 = request.POST.get('money')
    print(money1)
    return render(request)
# def handler(request):
#     text = request.POST.get('text')
#     print(text)
#     return render(request)